package com.jckl.foodathon.dao;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import org.restlet.engine.util.Base64;

import com.jckl.foodathon.model.Blogger;

public class BloggerDAO {

	public boolean newBlogger(Blogger blogger){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		boolean createdSuccess = false;
		
		try{
			pm.makePersistent(blogger);
			createdSuccess = true;
		}finally{
			pm.close();
		}
		return createdSuccess;
	}
	
	public List<Blogger> getBloggersByLatest(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Blogger.class);
		
		List<Blogger> bloggers = new ArrayList<Blogger>();

		try{
			query.setOrdering("updatedDate desc");
			bloggers = (List<Blogger>) query.execute();
			
		}finally{
			pm.close();
			query.closeAll();
		}
		return bloggers;
	}
	
	public List<Blogger> getBloggers(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Blogger.class);
		
		List<Blogger> bloggers = new ArrayList<Blogger>();
		List<Blogger> encodedBloggers = new ArrayList<Blogger>();
		
		try{
			query.setOrdering("name asc");
			bloggers = (List<Blogger>) query.execute();
			
		}finally{
			pm.close();
			query.closeAll();
		}
		
		for (Blogger blogger : bloggers) {
			if(blogger.getImage() != null){
				blogger.setEncodedImage(Base64.encode(blogger.getImage().getBytes(), false));
				blogger.setImage(null);
			}
			encodedBloggers.add(blogger);
		}
		
		return encodedBloggers;
	}
	
	public Blogger getBloggerByName(String bloggerName){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Blogger.class);
		
		List<Blogger> bloggers = null;
		Blogger blogger = null;

		try{
			query.setFilter("name == bloggerName");
			query.declareParameters("String bloggerName");
			bloggers = (List<Blogger>) query.execute(bloggerName);
			
			if(bloggers.size()>0){
				blogger = bloggers.get(0);
			}
		}catch (Exception ex){
			ex.printStackTrace();
		}finally{
			pm.close();
			query.closeAll();
		}
		return blogger;
	}
	
	public Blogger getBloggerByName(String bloggerName, Query query){
		if(bloggerName == null){
			return null;
		}
		
		List<Blogger> bloggers = null;
		Blogger blogger = null;

		try{
			query.setFilter("name == bloggerName");
			query.declareParameters("String bloggerName");
			bloggers = (List<Blogger>) query.execute(bloggerName);
			
			if(bloggers.size()>0){
				blogger = bloggers.get(0);
			}
		}catch (Exception ex){
			ex.printStackTrace();
		}
		return blogger;
	}

	public boolean updateBlogger (Blogger blogger){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Blogger.class);
		Transaction trx = pm.currentTransaction();
		Blogger persistedBlogger;
		boolean updatedSuccess = false;
		
		try{
			trx.begin();
			persistedBlogger = getBloggerByName(blogger.getName(), query);
			persistedBlogger.setAuthor(blogger.getAuthor());
			persistedBlogger.setBlogURL(blogger.getBlogURL());
			persistedBlogger.setImage(blogger.getImage());
			
			trx.commit();
			updatedSuccess = true;
		}finally{
			pm.close();
		}
		return updatedSuccess;
	}
}
